package hapi.util;

#if php
import php.FileSystem;
import php.Sys;
#elseif neko
import neko.FileSystem;
import neko.Sys;
#end

class FileStatHelper {
	
	var stat:FileStat;
	
	public function new(stat:FileStat) {
		this.stat = stat;
	}
	
	public function readable():String {
		var buf = new StringBuf();
		buf.add(size + " ");
		//TODO
		return buf.toString();
	}
	
	public function size() {
		return readableSize(stat.size);
	}
	
	public static function readableSize(bytes:Int):String {
		if ( bytes > 1024 * 1024 ) {
			var megaBytes = bytes / (1024 * 1024);
			return Math.round(megaBytes*100)/100 + " MB";
		}else if ( bytes > 1024 ) {
			var kiloBytes = bytes / 1024;
			return Math.round(kiloBytes) + " KB";
		}else
			return Math.round(bytes) + " B";
	}
	
	public static function resourceWithMtime( path:String, ?verParam="ver" ):String {
		return path + "?"+verParam+"=" + FileSystem.stat(Sys.getCwd()+path).mtime.getTime();
	}
	
	public function atime( ?format:String ) {
		return date( stat.atime, format );
	}
	
	public function mtime( ?format:String ) {
		return date( stat.mtime, format );
	}
	
	public function ctime( ?format:String ) {
		return date( stat.ctime, format );
	}
	
	function date(date:Date, ?format:String = "%Y-%M-%D %h:%m:%s") {
		return DateTools.format( date, format );
	}
}